<?php

/*
	spGuiListBoxNED : NED stands for: new, edit, delete
*/

require_once "tools/spGuid.php";
require_once "tools/spDebugProfiler.php";

class spGuiListBoxNED extends spDiv
{
	public function __construct( $param )
	{
		parent::__construct();
		
		parent::setClass( "spGuiListBoxNED" );
		
		if ( isset($param["id"]) )
			parent::setId( $param["id"] );
		else
		{
			// we create a random generated id
			$v4uuid = spGuid::v4();
			parent::setId( $v4uuid );
		}
		
		parent::addAttr( "data-ajaxinfo",			"ajaxInfo" );
		parent::addAttr( "data-infoselector",		$param["databaseinfo"] );					// this is the id of the element to show info
		parent::addAttr( "data-database",			$param["database"] );
		parent::addAttr( "data-formbuilder",		$param["formbuilder"] );
//		parent::addAttr( "data-database-title",		$param["databasename"] );					// title to be used for items
		parent::addAttr( "data-subheading",			$param["subheading"] );
		parent::addAttr( "data-formbuilderlayout", 	isset($param["formbuilderlayout"])?$param["formbuilderlayout"]:"" );
			
		parent::addChild( new spHeading(array(
			"content" 	=> $param["subheading"],
			"level"		=> "2"
		)));
		
		parent::addChild( new spDiv(array(
			"class" => "spListBoxInfo"
		)));

		parent::addChild( new spButtons(array(
			new spButton(array( 
				"class"		=> "spNew",
				"title" 	=> "New"
			)),		
			new spButton(array(
				"class"		=> "spEdit",
				"title" 	=> "Edit"
			)),
			new spButton(array(
				"class"		=> "spTrash",
				"title" 	=> "Trash"
			)),
			new spButton(array( 
				"class"		=> "spDelete",
				"title" 	=> "Delete"
			))		
		)));
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle listbox
///////////////////////////////////////////////////////////////////////////////////////////////////

function ajaxList()
{
	spProfileStart( "spGuiListBoxNED function ajaxList" );

	$dbclass = $_REQUEST["database"];

	$db = new $dbclass;
	
	spProfileStart( "spGuiListBoxNED function ajaxList getGuiListBoxItem2" );
	$ar = $db->getList();
	spProfileEnd( "spGuiListBoxNED function ajaxList getGuiListBoxItem2" );
	
	// we want the newest item on top
	//$ar = array_reverse( $ar );
	
	$width = $db->getMetaData( "guilistbox_width", "500px" );
	//echo "width = ".$width;
	
	$listbox = new spGuiListbox(array(
		"width" 	=> $width,
		"height"	=> "300px"
	));
	
	foreach ( $ar as $a )
	{
		$item = new spGuiListBoxItem(array(
			"content" => $a["content"]
		));
		
		if ( !isset($a["value"]) )
		{
			echo "ERROR: no value supplied for item in spGuiListBoxNED.php<br/>";
			echo "database: ".$dbclass."<br/>";
			spStackTrace();
			die();
		}
		
		$item->addClass( "updateinfo" );
		$item->addAttr(	"data-id", 		$a["value"] );
		$item->addAttr(	"data-name", 	$a["content"] );
		$listbox->addItem( $item );	
	}
	
	$listbox->render();
	
	spProfileEnd( "spGuiListBoxNED function ajaxList" );
	
	//spDebugProfiler::instance()->dump();
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle info
///////////////////////////////////////////////////////////////////////////////////////////////////

function ajaxInfo()
{
	$id = $_REQUEST["id"];
	$db = $_REQUEST["database"];
	$fb = $_REQUEST["formbuilder"];
	$fblayout = $_REQUEST["formbuilderlayout"];

	$c = new spDiv;
	
	$subheading = $_REQUEST["subheading"];
	$subheading = new spHeading(array(
		"level" => "2",
		"content" => $subheading." Info"
	));
	$c->addChild( $subheading );
	$c->render();

	$database = new $db( $id );
	$formBuilder = new $fb( $database );
	
	if ( $fblayout!="" )
		$formBuilder->setFormLayoutDefinition( $fblayout() );	
	
	$formBuilder->showSubTitles( false );
	$formBuilder->showRequired( false );
	$formBuilder->setReadOnly( true );
	$formBuilder->render();
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle trash
///////////////////////////////////////////////////////////////////////////////////////////////////

function spGuiListBoxNEDTrash()
{
	$id = $_REQUEST["id"];
	$db = $_REQUEST["database"];

	$obj = new $db( $id );
	if ( !$obj->trash() )
	{
		echo "spGuiListBoxNEDTRASH failed";
		var_dump( $_REQUEST );
		return;
	}
	
	echo "{}";
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle delete
///////////////////////////////////////////////////////////////////////////////////////////////////

function ajaxDelete()
{
	$id = $_REQUEST["id"];
	$db = $_REQUEST["database"];

	$obj = new $db( $id );
	if ( !$obj->delete() )
	{
		echo '{"errors":["delete failed"]}';
		return;
	}
	
	echo "{}";
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle save
///////////////////////////////////////////////////////////////////////////////////////////////////

function ajaxNewForm()
{
	$db 		= $_REQUEST["database"];
	$fb 		= $_REQUEST["formbuilder"];
	$listboxId 	= $_REQUEST["listboxid"];	// id of listbox that this form belongs to

	$c = new spDiv;
	
	$subheading = $_REQUEST["subheading"];
	$subheading = new spHeading(array(
		"level" => "2",
		"content" => "New: ".$subheading
	));
	$c->addChild( $subheading );
	
	$database = new $db;
	$formBuilder = new $fb( $database );
	$formBuilder->addAttr( "data-listboxid", $listboxId );
	$formBuilder->addAttr( "data-callbacksave", "ajaxNewSave" );
	$formBuilder->addChild( new spButtons(array(
		new spButton(array( 
			"class"		=> "spSave",
			"title" 	=> "Save"
		))		
	)));	
	$c->addChild( $formBuilder );
	
	$c->render();
}

// called when the user presses save
function ajaxNewSave()
{
	$db = $_REQUEST["database"];
	
	$database = new $db;
	$database->updateFromRequest();
	if ( !$database->save() )
	{
		$errors = $database->getFormErrors();
		$json = array(
			"formerrors" => $errors
		);
		echo json_encode( $json );
	}
	else
	{
		echo '{"redirect":"noredirect"}';
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle edit
///////////////////////////////////////////////////////////////////////////////////////////////////

function ajaxEditForm()
{
	$db 		= $_REQUEST["database"];
	$fb 		= $_REQUEST["formbuilder"];
	$listboxId 	= $_REQUEST["listboxid"];	// id of listbox that this form belongs to
	$id 		= $_REQUEST["id"];

	$c = new spDiv;
	
	$subheading = $_REQUEST["subheading"];
	$subheading = new spHeading(array(
		"level" => "2",
		"content" => "Editing ".$subheading
	));
	$c->addChild( $subheading );
	
	$database = new $db($id);
	$formBuilder = new $fb( $database );
	$formBuilder->addAttr( "data-listboxid", $listboxId );
	$formBuilder->addAttr( "data-callbacksave", "ajaxEditSave" );
	$formBuilder->addChild( new spButtons(array(
		new spButton(array( 
			"class"		=> "spSave",
			"title" 	=> "Save"
		))		
	)));	
	$c->addChild( $formBuilder );
	
	$c->render();
}

function ajaxEditSave()
{
	$_REQUEST = unMagicQuotify($_REQUEST);

	$db = $_REQUEST["database"];
	$id = $_REQUEST["id"];

	$database = new $db( $id );
	$database->updateFromRequest();
	if ( !$database->save() )
	{
		$errors = $database->getFormErrors();
		$json = array(
			"formerrors" => $errors
		);
		echo json_encode( $json );
	}
	else
	{
		echo '{"redirect":"noredirect"}';
	}	
}

?>